﻿package com.athena.process
{
	import flash.display.MovieClip;
	import flash.display.*;
	import flash.utils.*;
	import flash.events.*;
	import flash.text.TextFormat;
	import com.adobe.serialization.json.JSON;
	/**
	 * ...
	 * @author ...
	 */
	public class BankFunction 
	{
		public static function Transfer_Click(evt : MouseEvent) {
			var mc : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Bank")) ;
			if (mc == null) return ;
			mc.TransferContent_mc.visible = true ;
			mc.LoanContent_mc.visible = false ;
			mc.HistoryContent_mc.visible = false ;
			mc.Transfer_btn.gotoAndStop(2) ;
			mc.Loan_btn.gotoAndStop(1) ;
			mc.History_btn.gotoAndStop(1) ;
		}
		public static function Loan_Click(evt : MouseEvent) {
			var mc : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Bank")) ;
			if (mc == null) return ;
			mc.TransferContent_mc.visible = false ;
			mc.LoanContent_mc.visible = true ;
			mc.HistoryContent_mc.visible = false ;
			mc.Transfer_btn.gotoAndStop(1) ;
			mc.Loan_btn.gotoAndStop(2) ;
			mc.History_btn.gotoAndStop(1) ;
		}
		public static function History_Click(evt : MouseEvent) {
			var mc : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Bank")) ;
			if (mc == null) return ;
			mc.TransferContent_mc.visible = false ;
			mc.LoanContent_mc.visible = false ;
			mc.HistoryContent_mc.visible = true ;
			mc.Transfer_btn.gotoAndStop(1) ;
			mc.Loan_btn.gotoAndStop(1) ;
			mc.History_btn.gotoAndStop(2) ;
		}
		public static function Bank_Click(evt : MouseEvent) { //Chuyen AG
			Cocaro.SoundKick() ;
			if ((Cocaro.mainStage_mc.currentFrame != 3) && (Cocaro.mainStage_mc.currentFrame != 4)) return ;
			var mc : MovieClip  ;
			mc = MovieClip(new Popup_Bank()) ;				
			var formatText : TextFormat = new TextFormat() ;				
			formatText.font = "Tahoma" ;
			formatText.bold = true ;
			formatText.color = 0xFFFFFF ;
			formatText.size = 14 ;
			mc.TransferContent_mc.rb1.setStyle("textFormat", formatText) ;
			mc.TransferContent_mc.rb2.setStyle("textFormat", formatText) ;
			mc.TransferContent_mc.rb3.setStyle("textFormat", formatText) ;
			mc.TransferContent_mc.rb4.setStyle("textFormat", formatText) ;
			mc.TransferContent_mc.rb5.setStyle("textFormat", formatText) ;
			mc.TransferContent_mc.rb6.setStyle("textFormat", formatText) ;
			mc.TransferContent_mc.rb7.setStyle("textFormat", formatText) ;
			mc.TransferContent_mc.rb8.setStyle("textFormat", formatText) ;
			mc.TransferContent_mc.rb1.enabled = true ;
			mc.TransferContent_mc.rb2.enabled = false ;
			mc.TransferContent_mc.rb3.enabled = false ;
			mc.TransferContent_mc.rb4.enabled = false ;
			mc.TransferContent_mc.rb5.enabled = false ;
			mc.TransferContent_mc.rb6.enabled = false ;
			mc.TransferContent_mc.rb7.enabled = false ;
			mc.TransferContent_mc.rb8.enabled = false ;
			var tfStyle:TextFormat = new TextFormat();
			tfStyle.size = 12;
			tfStyle.color = 0xFFFFFF;
			tfStyle.font = "Arial";
			mc.TransferContent_mc.name_txt.setStyle("textFormat", tfStyle);
			mc.TransferContent_mc.TransferFee_txt.text = '10%' ;
			if (Cocaro.currPlayer.VIP == 1)
				mc.TransferContent_mc.TransferFee_txt.text = '8%' ;
			if (Cocaro.currPlayer.VIP > 1) {
				mc.TransferContent_mc.rb2.enabled = true ;				
				mc.TransferContent_mc.TransferFee_txt.text = '6%' ;
			}
			if (Cocaro.currPlayer.VIP > 3) {
				mc.TransferContent_mc.rb3.enabled = true ;
				mc.TransferContent_mc.TransferFee_txt.text = '5%' ;
				if (Cocaro.currPlayer.VIP > 5)
					mc.TransferContent_mc.TransferFee_txt.text = '4%' ;
			}
			if (Cocaro.currPlayer.VIP > 5)
				mc.TransferContent_mc.rb4.enabled = true ;				
			if (Cocaro.currPlayer.VIP > 6) {
				mc.TransferContent_mc.rb5.enabled = true ;
				mc.TransferContent_mc.TransferFee_txt.text = '3%' ;				
			}
			if (Cocaro.currPlayer.VIP > 7) {
				mc.TransferContent_mc.rb6.enabled = true ;
				mc.TransferContent_mc.TransferFee_txt.text = '2%' ;
			}
			if (Cocaro.currPlayer.VIP > 8) {
				mc.TransferContent_mc.rb7.enabled = true ;
				mc.TransferContent_mc.TransferFee_txt.text = '1%' ;
			}
			if (Cocaro.currPlayer.VIP > 9) {
				mc.TransferContent_mc.rb8.enabled = true ;
				mc.TransferContent_mc.TransferFee_txt.text = '0%' ;
			}
			mc.TransferContent_mc.rb1.selected = true ;
			mc.TransferContent_mc.TransferFee_txt.setTextFormat(formatText) ;
			mc.TransferContent_mc.btnOK_mc.addEventListener(MouseEvent.CLICK, OK_click);			
			mc.TransferContent_mc.btnNotOK_mc.addEventListener(MouseEvent.CLICK, NotOK_click);
			mc.Close_btn.addEventListener(MouseEvent.CLICK, Close_click);
			mc.TransferContent_mc.visible = true ;
			mc.LoanContent_mc.visible = false ;
			mc.HistoryContent_mc.visible = false ;
			mc.Transfer_btn.gotoAndStop(2) ;
			mc.Loan_btn.gotoAndStop(1) ;
			mc.History_btn.gotoAndStop(1) ;
			mc.History_btn.visible = false ;
			
			//Loan AG			
			mc.LoanContent_mc.rbLoan1.setStyle("textFormat", formatText) ;
			mc.LoanContent_mc.rbLoan2.setStyle("textFormat", formatText) ;
			mc.LoanContent_mc.rbLoan3.setStyle("textFormat", formatText) ;
			mc.LoanContent_mc.rbLoan4.setStyle("textFormat", formatText) ;
			mc.LoanContent_mc.rbLoan5.setStyle("textFormat", formatText) ;
			mc.LoanContent_mc.rbLoan6.setStyle("textFormat", formatText) ;
			mc.LoanContent_mc.rbLoan1.enabled = false ;
			mc.LoanContent_mc.rbLoan2.enabled = false ;
			mc.LoanContent_mc.rbLoan3.enabled = false ;
			mc.LoanContent_mc.rbLoan4.enabled = false ;
			mc.LoanContent_mc.rbLoan5.enabled = false ;
			mc.LoanContent_mc.rbLoan6.enabled = false ;
			mc.LoanContent_mc.Pay_btn.visible = false ;
			mc.LoanContent_mc.AlertLoan_txt.visible = false ;
			mc.LoanContent_mc.AGLoan_txt.visible = false ;
			if (Cocaro.currPlayer.LoanAG > 0) {
				mc.LoanContent_mc.Alert_txt.visible = false ;
				mc.LoanContent_mc.rbLoan1.visible = false ;
				mc.LoanContent_mc.rbLoan2.visible = false ;
				mc.LoanContent_mc.rbLoan3.visible = false ;
				mc.LoanContent_mc.rbLoan4.visible = false ;
				mc.LoanContent_mc.rbLoan5.visible = false ;
				mc.LoanContent_mc.rbLoan6.visible = false ;
				mc.LoanContent_mc.btnOK_mc.visible = false ;
				mc.TransferContent_mc.btnOK_mc.visible = false ;
				mc.TransferContent_mc.btnNotOK_mc.visible = false ;
				mc.LoanContent_mc.Pay_btn.visible = true ;
				mc.LoanContent_mc.AlertLoan_txt.visible = true ;
				mc.LoanContent_mc.AGLoan_txt.visible = true ;				
				mc.LoanContent_mc.AGLoan_txt.text = BasicFunction.numberFormat(Cocaro.currPlayer.LoanAG, 0, false, true) + " AG" ;
			} else {
				mc.LoanContent_mc.Alert_txt.visible = true ;
				mc.LoanContent_mc.rbLoan1.visible = true ;
				mc.LoanContent_mc.rbLoan2.visible = true ;
				mc.LoanContent_mc.rbLoan3.visible = true ;
				mc.LoanContent_mc.rbLoan4.visible = true ;
				mc.LoanContent_mc.rbLoan5.visible = true ;
				mc.LoanContent_mc.btnOK_mc.visible = true ;
				mc.LoanContent_mc.rbLoan1.selected = true ;	
				if (Cocaro.currPlayer.VIP > 4) 
					mc.LoanContent_mc.rbLoan1.enabled = true ;
				if (Cocaro.currPlayer.VIP > 5) 
					mc.LoanContent_mc.rbLoan2.enabled = true ;
				if (Cocaro.currPlayer.VIP > 6)
					mc.LoanContent_mc.rbLoan3.enabled = true ;
				if (Cocaro.currPlayer.VIP > 7)
					mc.LoanContent_mc.rbLoan4.enabled = true ;
				if (Cocaro.currPlayer.VIP > 8)
					mc.LoanContent_mc.rbLoan5.enabled = true ;
				if (Cocaro.currPlayer.VIP > 9)
					mc.LoanContent_mc.rbLoan6.enabled = true ;
			}
			mc.LoanContent_mc.btnOK_mc.addEventListener(MouseEvent.CLICK, ShowConfirm);
			mc.LoanContent_mc.Pay_btn.addEventListener(MouseEvent.CLICK, Pay_Click);
			mc.Transfer_btn.addEventListener(MouseEvent.CLICK, Transfer_Click) ;
			mc.Loan_btn.addEventListener(MouseEvent.CLICK, Loan_Click) ;
			mc.History_btn.addEventListener(MouseEvent.CLICK, History_Click) ;
			if ((Cocaro.currPlayer.VIP < 5) && (Cocaro.currPlayer.LoanAG == 0)) {
				mc.Transfer_btn.visible = false ;
				mc.Loan_btn.visible = false ;
				mc.History_btn.visible = false ;
			}
			mc.name = "Bank" ;
			mc.x = 0 ;
			mc.y = 0 ;
			Cocaro.mainStage_mc.addChild(mc) ;
		}
		public static function OK_click(evt:MouseEvent) { //Xu ly Kick Xac nhan tren popup chuyen tien
			Cocaro.SoundKick()
			var mc : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Bank")) ;
			if (mc == null) return ;
			var strName : String = mc.TransferContent_mc.name_txt.text ;			
			var rbg : Object = mc.TransferContent_mc.rb1.group ;
			if (rbg.selection == null) {
				BasicFunction.ShowAlert("Phải chọn số AG cần chuyển!") ;
				return ;
			}
			if (strName.length > 100)
				strName = strName.substring(0, 100) ;
			strName = BasicFunction.trim(strName) ;
			if (strName.length < 1) {
				BasicFunction.ShowAlert("Phải nhập tên Tài khoản nhận!") ;
				return ;
			}
			if ((strName == Cocaro.currPlayer.PlayerName) && (Cocaro.currPlayer.VIP < 5)) {
				BasicFunction.ShowAlert("Bạn phải là Vip 5 trở lên mới được cất AG!") ;
				return ;
			}
			var object : Object = new Object() ;
			object.AG = rbg.selection.value ;
			object.Name = strName ;
			Cocaro.AGTransfer = object.AG ;
			if ((Cocaro.currPlayer.Mark <= object.AG) || (strName == "")) {
				if (strName == "")
					BasicFunction.ShowAlert("Tài khoản nhận phải khác rỗng!") ;
				else 
					BasicFunction.ShowAlert("Bạn không đủ AG để chuyển!") ;
				return ;
			}
			object.evt = "10";
			Cocaro.mainSocket.sendService(JSON.encode(object));
			//Cocaro.tf.appendText("==>SendTransfer:" + JSON.encode(object)) ;
			//Hien thi Man hinh Loading
			var tmpLD:MovieClip = MovieClip(new Loading());
			tmpLD.name = "Loading";
			tmpLD.x = 0;
			tmpLD.y = 0;
			Cocaro.mainStage_mc.addChild(tmpLD);
			mc.TransferContent_mc.name_txt.text = "" ;	
		}
		public static function NotOK_click(evt:MouseEvent) { //Xu ly Kick Huy bo tren popup chuyen tien
			Cocaro.SoundKick() ;
			var tmpLD : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Loading"));
			while (tmpLD != null) {
				Cocaro.mainStage_mc.removeChild(tmpLD) ;
				tmpLD = MovieClip(Cocaro.mainStage_mc.getChildByName("Loading"));
			}
			var mc : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Bank")) ;
			if (mc == null) return ;
			Cocaro.mainStage_mc.removeChild(mc) ;			
		}
		public static function Close_click(evt:MouseEvent) { //Xu ly Kick Close tren popup chuyen tien
			Cocaro.SoundKick() ;
			var tmpLD : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Loading"));
			while (tmpLD != null) {
				Cocaro.mainStage_mc.removeChild(tmpLD) ;
				tmpLD = MovieClip(Cocaro.mainStage_mc.getChildByName("Loading"));
			}
			var mc : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Bank")) ;
			if (mc == null) return ;
			Cocaro.mainStage_mc.removeChild(mc) ;			
		}
		//Nhan duoc thong bao chuyen AG thanh cong
		public static function ResultTransferAG(str : String) { //Ham xu ly sau khi nhan thong bao chuyen ag thanh cong
			//Cocaro.tf.appendText("==>TransferAG:" + str) ;
			var tmpLD : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Loading"));
			while (tmpLD != null) {
				Cocaro.mainStage_mc.removeChild(tmpLD) ;
				tmpLD = MovieClip(Cocaro.mainStage_mc.getChildByName("Loading"));
			}
			Cocaro.currPlayer.Mark = Cocaro.currPlayer.Mark - Cocaro.AGTransfer ;
			Cocaro.AGTransfer = 0 ;
			if ((Cocaro.mainStage_mc.currentFrame == 4) || (Cocaro.mainStage_mc.currentFrame == 3)) {
				Cocaro.mainStage_mc.Profile_mc.Mark_txt.text = BasicFunction.numberFormat(Cocaro.currPlayer.Mark, 0, false, true) + " AG";
				Cocaro.mainStage_mc.Profile_mc.Mark_txt.setTextFormat(Cocaro.formatTextMain) ;
			}
			BasicFunction.ShowAlert(str) ;			
		}
		
		public static function ResultLoanAG(AG : Number) {//Ham xu ly sau khi nhan thong bao vay ag thanh cong
			//Cocaro.tf.appendText("=>Vay:" + AG) ;
			var mc : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Bank")) ;
			if (mc == null) return ;
			mc.LoanContent_mc.Alert_txt.visible = false ;
			mc.LoanContent_mc.rbLoan1.visible = false ;
			mc.LoanContent_mc.rbLoan2.visible = false ;
			mc.LoanContent_mc.rbLoan3.visible = false ;
			mc.LoanContent_mc.rbLoan4.visible = false ;
			mc.LoanContent_mc.rbLoan5.visible = false ;
			mc.LoanContent_mc.rbLoan6.visible = false ;
			mc.LoanContent_mc.btnOK_mc.visible = false ;
			mc.LoanContent_mc.Pay_btn.visible = true ;
			mc.LoanContent_mc.AlertLoan_txt.visible = true ;
			mc.LoanContent_mc.AGLoan_txt.visible = true ;
			mc.LoanContent_mc.AGLoan_txt.setTextFormat(Cocaro.formatTextMain) ;
			mc.LoanContent_mc.AGLoan_txt.text = BasicFunction.numberFormat(AG, 0, false, true) ;
			mc.TransferContent_mc.btnOK_mc.visible = false ;
			mc.TransferContent_mc.btnNotOK_mc.visible = false ;
			Cocaro.currPlayer.Mark = Cocaro.currPlayer.Mark + AG ;
			if ((Cocaro.mainStage_mc.currentFrame == 4) || (Cocaro.mainStage_mc.currentFrame == 3)) {
				Cocaro.mainStage_mc.Profile_mc.Mark_txt.text = BasicFunction.numberFormat(Cocaro.currPlayer.Mark, 0, false, true) + " AG";
				Cocaro.mainStage_mc.Profile_mc.Mark_txt.setTextFormat(Cocaro.formatTextMain) ;
			}
			Cocaro.currPlayer.LoanAG = AG ;
			BasicFunction.ShowAlert("Bạn đã vay AG thành công, nợ của bạn hiện tại là " + BasicFunction.numberFormat(Cocaro.currPlayer.LoanAG, 0, false, true)) ;
			BasicFunction.RemoveHideLoading() ;
		}
		public static function ResultPayAG() {//Ham xu ly sau khi nhan thong bao tra ag thanh cong
			//Cocaro.tf.appendText("==>TraAG:") ;
			var mc : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Bank")) ;
			if (mc == null) return ;
			mc.LoanContent_mc.Alert_txt.visible = true ;
			mc.LoanContent_mc.rbLoan1.visible = true ;
			mc.LoanContent_mc.rbLoan2.visible = true ;
			mc.LoanContent_mc.rbLoan3.visible = true ;
			mc.LoanContent_mc.rbLoan4.visible = true ;
			mc.LoanContent_mc.rbLoan5.visible = true ;
			mc.LoanContent_mc.rbLoan6.visible = true ;
			mc.LoanContent_mc.btnOK_mc.visible = true ;
			mc.LoanContent_mc.rbLoan1.selected = true ;	
			if (Cocaro.currPlayer.VIP > 4) 
				mc.LoanContent_mc.rbLoan1.enabled = true ;
			if (Cocaro.currPlayer.VIP > 5) 
				mc.LoanContent_mc.rbLoan2.enabled = true ;
			if (Cocaro.currPlayer.VIP > 6)
				mc.LoanContent_mc.rbLoan3.enabled = true ;
			if (Cocaro.currPlayer.VIP > 7)
				mc.LoanContent_mc.rbLoan4.enabled = true ;
			if (Cocaro.currPlayer.VIP > 8)
				mc.LoanContent_mc.rbLoan5.enabled = true ;
			if (Cocaro.currPlayer.VIP > 9)
				mc.LoanContent_mc.rbLoan6.enabled = true ;
			mc.LoanContent_mc.Pay_btn.visible = false ;
			mc.LoanContent_mc.AlertLoan_txt.visible = false ;
			mc.LoanContent_mc.AGLoan_txt.visible = false ;
			mc.TransferContent_mc.btnOK_mc.visible = true ;
			mc.TransferContent_mc.btnNotOK_mc.visible = true ;
			Cocaro.currPlayer.Mark = Cocaro.currPlayer.Mark - Cocaro.currPlayer.LoanAG ;
			Cocaro.currPlayer.LoanAG = 0 ;
			if ((Cocaro.mainStage_mc.currentFrame == 4) || (Cocaro.mainStage_mc.currentFrame == 3)) {
				Cocaro.mainStage_mc.Profile_mc.Mark_txt.text = BasicFunction.numberFormat(Cocaro.currPlayer.Mark, 0, false, true) + " AG";
				Cocaro.mainStage_mc.Profile_mc.Mark_txt.setTextFormat(Cocaro.formatTextMain) ;
			}
			BasicFunction.ShowAlert("Bạn đã trả AG thành công!") ;
			BasicFunction.RemoveHideLoading() ;
		}
		/************** Show and Hide Popup Confirm ****************************/
		public static function RemovePopupConfirm() {
			var mc : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Confirm")) ;
			while (mc != null) {
				Cocaro.mainStage_mc.removeChild(mc) ;
				mc = MovieClip(Cocaro.mainStage_mc.getChildByName("Confirm")) ;
			}
		}
		public static function Loan_OK_click(evt : MouseEvent) {
			var mc : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Bank")) ;
			if (mc == null) return ;
			var rb : Object = mc.LoanContent_mc.rbLoan1.group ;
			if (rb == null) return ;
			//Cocaro.tf.appendText("=>Loan:" + rb.selection.value) ;
			//Hien thi Popup thong bao hoi vay
			var obj : Object = new Object() ;
			obj.evt = "14" ;
			obj.T = 1 ;
			obj.AG = rb.selection.value ;
			//Cocaro.tf.appendText("=>SendVay:" + JSON.encode(obj)) ;
			Cocaro.mainSocket.sendService(JSON.encode(obj)) ;
			RemovePopupConfirm() ;
			var tmpLD:MovieClip = MovieClip(new Loading());
			tmpLD.name = "Loading";
			tmpLD.x = 0;
			tmpLD.y = 0;			
			Cocaro.mainStage_mc.addChild(tmpLD);
		}
		public static function Loan_NotOK_click(evt : MouseEvent) {
			RemovePopupConfirm() ;
		}
		public static function ShowConfirm(evt : MouseEvent) {
			if (Cocaro.currPlayer.VIP < 5) {
				BasicFunction.ShowAlert("Bạn không đủ cấp độ để vay AG!") ;
				return ;
			}
			var mc : MovieClip = MovieClip(new Popup_Confirm()) ;
			mc.Title_txt.text = "Vay AG" ;
			mc.Content_txt.text = "Bạn có chắc chắn muốn vay AG?" ;
			mc.OK_btn.addEventListener(MouseEvent.CLICK, Loan_OK_click) ;
			mc.NotOK_btn.addEventListener(MouseEvent.CLICK, Loan_NotOK_click) ;
			mc.name = "Confirm" ;
			mc.x = 0 ;
			mc.y = 0 ;
			Cocaro.mainStage_mc.addChild(mc) ;
		}
		public static function Pay_Click(evt : MouseEvent) {
			if (Cocaro.currPlayer.Mark > Cocaro.currPlayer.LoanAG) {
				var obj : Object = new Object() ;
				obj.evt = "14";
				obj.T = 2;
				//Cocaro.tf.appendText("=>SendTraAG:" + JSON.encode(obj)) ;
				Cocaro.mainSocket.sendService(JSON.encode(obj));
				var tmpLD:MovieClip = MovieClip(new Loading());
				tmpLD.name = "Loading";
				tmpLD.x = 0;
				tmpLD.y = 0;			
				Cocaro.mainStage_mc.addChild(tmpLD);
			} else BasicFunction.ShowAlert("Bạn chưa đủ AG để trả!") ;
		}		
	}	
}